package com.puzzlebazar.server;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.puzzlebazar.client.services.UserInfoService;
import com.puzzlebazar.client.user.UserInfo;
import com.puzzlebazar.client.util.Capabilities;


public class UserInfoServiceImpl extends MainServiceServlet implements
    UserInfoService {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4236893720888570772L;

	@Override
	public UserInfo loadUser( String email ) {
		
		if( !Capabilities.canSeeUser(getCurrentUserInfo(),email) )
			return null;

	    final PersistenceManager pm = PMF.get().getPersistenceManager();
	    UserInfoJdo jdo;
	    UserInfo result = new UserInfo();
	    try {
	    	jdo = pm.getObjectById(UserInfoJdo.class, email);
	    	jdo.to( result );
	    } catch( JDOObjectNotFoundException exception ) {
	    	return null;
	    }
	    finally {
	        pm.close();
	    }
		
		return result;
	}

	@Override
	public UserInfo loadCurrentUser() {
		
		HttpSession session = getSession( true );
		UserService userService = UserServiceFactory.getUserService();
		
		if( !userService.isUserLoggedIn() ) {
			session.removeAttribute( USERINFO_ATTRIBUTE );
			return null;
		}
		User user = userService.getCurrentUser();		
		
	    final PersistenceManager pm = PMF.get().getPersistenceManager();
	    UserInfoJdo jdo;
	    UserInfo result = new UserInfo();
	    try {
	    	jdo = pm.getObjectById(UserInfoJdo.class, user.getEmail());
	    	jdo.to( result );
	    	if( result.isAdministrator() !=  userService.isUserAdmin() )
	    		result.setAdministrator( userService.isUserAdmin() );	    	
	    } catch( JDOObjectNotFoundException exception ) {
	    	result.setEmail( user.getEmail() );
	    	result.setNickname( user.getNickname() );
	    	result.setAdministrator( userService.isUserAdmin() );
	    }
	    finally {
	        pm.close();
	    }

		session.setAttribute( USERINFO_ATTRIBUTE, result );
		
		return result;
	}

	@Override
	public int modifyUser(UserInfo userInfo) {


		if( !Capabilities.canModifyUser(getCurrentUserInfo(),userInfo.getEmail()) )
			return UserInfo.ErrorFlags.MODIFYING_OTHER_USER;

		UserService userService = UserServiceFactory.getUserService();
		
		// Override userInfo.administrator based on user status
		userInfo.setAdministrator( 
				userService.isUserAdmin() &&
				userService.getCurrentUser().getEmail().equals( userInfo.getEmail() ) );

		final PersistenceManager pm = PMF.get().getPersistenceManager();
		UserInfoJdo jdo = new UserInfoJdo();
		jdo.from( userInfo );
		try {
			pm.makePersistent( jdo );
		} catch( JDOObjectNotFoundException exception ) {
			return UserInfo.ErrorFlags.UNREGISTERED_EMAIL;
		}
		finally {
			pm.close();
		}		
		
		if( userService.isUserLoggedIn() &&
				userService.getCurrentUser().getEmail().equals( userInfo.getEmail() ) ) {

			userInfo.setAdministrator( userService.isUserAdmin() );
			HttpSession session = getSession( true );
			session.setAttribute( USERINFO_ATTRIBUTE, userInfo );
			
		}

		return UserInfo.ErrorFlags.SUCCESS;
	}
	

}
